package net.dntuan.training.spring.integration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.target.CommonsPoolTargetSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author tuan.dangnhu
 * 
 * $LastChangedRevision: 111 $
 * $LastChangedDate: 2014-07-02 03:37:19 +0000 (Wed, 02 Jul 2014) $
 * $LastChangedBy: dnhutuan@gmail.com $
 */
@Service
public class BusinessService {

  private static final Logger logger = LoggerFactory.getLogger(BusinessService.class);

  @Autowired
  private CommonsPoolTargetSource poolTargetSource;

  public BusinessService() {
  }

  public void doSomething() {
    try {
      SimpleBean bean = (SimpleBean) poolTargetSource.getTarget();
      logger.info("hashcode of bean: " + bean.hashCode() + ", thread id: " + Thread.currentThread().getId());
      Thread.sleep(Math.round(Math.random() * 1000));
      poolTargetSource.releaseTarget(bean);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}
